package com.hxkcloud.nurse.utils;

import android.content.Context;
import android.text.TextUtils;

import com.hxkcloud.nurse.beans.DepartmentBean;
import com.hxkcloud.nurse.dborm.DepartmentDao;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by william_sun on 2016-07-05.
 * Description:文字操作工具类
 */
public class StringUtil {
    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input) || "null".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断是否为手机号
     */
    public static boolean isTelephonenum(String input) {
//        if (input.length() == 11) {
//            for (int i = 0; i < 11; i++) {
//                if (!((((int) (input.charAt(i))) >= 48) && (((int) (input.charAt(i))) <= 57))) {
//                    return false;
//                }
//            }
//            return true;
//        } else {
//            return false;
//        }
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(input);
        b = m.matches();
        return b;
    }

    /**
     * 全角字符和半角字符混乱使用
     */
    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    /**
     * 计算工龄或年龄
     */
    public static int getAgeFromBirthday(Date birthday) {
        if (birthday == null) {
            return -1;
        }
        Calendar calendar = Calendar.getInstance();
        if (birthday.after(calendar.getTime())) {
            return 0;
        }
        int yearNow = calendar.get(Calendar.YEAR);
        int monthNow = calendar.get(Calendar.MONTH) + 1;
        calendar.setTime(birthday);
        int age = yearNow - calendar.get(Calendar.YEAR);
        if (monthNow < (calendar.get(Calendar.MONTH) + 1)) {
            age -= 1;
        }
        return age;
    }

    public static int getAgeFromBirthday(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        int yearNow = calendar.get(Calendar.YEAR);
        int monthNow = calendar.get(Calendar.MONTH) + 1;
        int age = yearNow - year;
        if (monthNow < month) {
            age -= 1;
        }
        return age;
    }

    //判断当前登录用户是不是产科用户
    public static boolean isCurChanke(String departmentId, Context mContext) {
        boolean isChanke = false;
        if (!TextUtils.isEmpty(departmentId)) {
            DepartmentDao departmentDao = new DepartmentDao(mContext);
            List<DepartmentBean> dbs = departmentDao.getDepartment();
            for (DepartmentBean d : dbs) {
                if (d.getDepartmentId() == Integer.valueOf(departmentId)) {
                    if (d.getType().equals("0")) {//产科
                        isChanke = true;
                        break;
                    }
                }
            }
        }
        return isChanke;
    }

    //诸葛IO埋点
    public static void zhugeTrack(Context context, String arg1, String arg2) {
        //定义与事件相关的属性信息
        JSONObject eventObject = new JSONObject();
        try {
            eventObject.put(arg2, "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
